/*
   Copyright (c) 2020 Shanghai Xuanzi Technology Co. Ltd https://xuanzi.ltd
   XZUnitTest is licensed under the Mulan PSL v1.
   You can use this software according to the terms and conditions of the Mulan PSL v1.
   You may obtain a copy of Mulan PSL v1 at:
      http://license.coscl.org.cn/MulanPSL
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
   PURPOSE.
   See the Mulan PSL v1 for more details. 
 */

package xuanzi.h5.unittest;

import java.util.ArrayList;
import java.util.List;

public class Asserts {
	
	public static int errorTotal ;
	
	public static int passTotal;
	
	public static interface Countable{
		public void pass(String message);
		public void error(String message);
	}
	
	public static List<Countable> countables = new ArrayList<Asserts.Countable>();
	
	static{
		Console.log("Welcome "+BuildConfig.MODULE_NAME+" "+BuildConfig.VERSION_NAME+" author site: https://xuanzi.ltd");
	}
	public Asserts() {
		// TODO Auto-generated constructor stub
	}
	
 
	
	public static void assertTrue(String message,boolean result) { 
		if(result) { 
			passInc(message);
			pass(message);
		}else {
			errorInc(message);
			error(message);
		}
	}
	
	private static void errorInc(String message) {
		errorTotal++;
		for(Countable c:countables)
			c.error(message);
	}



	private static void passInc(String message) {
		passTotal++;
		for(Countable c:countables)
			c.pass(message);
	}



	public static void assertNull(String message,Object result) { 
		if(result==null) {
			passInc(message);
			pass(message);
		}else {
			errorInc(message);
			error(message);
		}
	}
	
	public static void assertNotNull(String message,Object result) { 
		if(result!=null) {
			passInc(message);
			pass(message);
		}else {
			errorInc(message);
			error(message);
		}
	}
	
	public static void assertFalse(String message,boolean result) { 
		if(!result) {
			passInc(message);
			pass(message);
		}else {
			errorInc(message);
			error(message);
		}
	}
	
	
	public static void assertNotEquals(String message,int ext,int eft) { 
		if(!(ext==eft)) {
			passInc(message);
			pass(message);
		}else {
			errorInc(message);
			error(message+" [" +ext +"] == ["+eft+"]");
		}
	}
	
	public static void assertNotEquals(String message,boolean ext,boolean eft) { 
		if(!(ext==eft)) {
			passInc(message);
			pass(message);
		}else {
			errorInc(message);
			error(message+" [" +ext +"] == ["+eft+"]");
		}
	}
	
	public static void assertNotEquals(String message,Object ext,Object eft) { 
		if(!ext.equals(eft)) {
			passInc(message);
			pass(message);
		}else {
			errorInc(message);
			error(message+" [" +ext +"] == ["+eft+"]");
		}
	}
	
	public static void assertEquals(String message,int ext,int eft) { 
		if(ext==eft) {
			passInc(message);
			pass(message);
		}else {
			errorInc(message);
			error(message+" [" +ext +"] != ["+eft+"]");
		}
	}
	
	public static void assertEquals(String message,Object ext,Object eft) { 
		if(ext==eft||ext.equals(eft)) {
			passInc(message);
			pass(message);
		}else {
			errorInc(message);
			error(message+" [" +ext +"] != ["+eft+"]");
		}
	} 

	
	public static native void pass(Object info)/*-{
	 
		console.log("  %c"+info,"color:green;border-left:16px solid green;padding-left:8px;");
	}-*/;
	
	public static native void debug(Object info)/*-{
		console.error("[DEBUG] "+info);
	}-*/;
	
	public static native void error(Object info)/*-{
		console.error("❌ "+info);
	}-*/;
	
	public static native void warn(String info)/*-{
		console.warn(info);
	}-*/;
	
	public static native void log(Object info,String css)/*-{
		console.log(info,css);
	}-*/;



	public native static void initNative()/*-{
	  $wnd.Asserts = {
	  	assertNull : @xuanzi.h5.unittest.Asserts::assertNull(Ljava/lang/String;Ljava/lang/Object;),
	  	assertNotNull : @xuanzi.h5.unittest.Asserts::assertNotNull(Ljava/lang/String;Ljava/lang/Object;),
	  	assertFalse:@xuanzi.h5.unittest.Asserts::assertFalse(Ljava/lang/String;Z),
	  	assertTrue:@xuanzi.h5.unittest.Asserts::assertTrue(Ljava/lang/String;Z),
	  	assertEquals : @xuanzi.h5.unittest.Asserts::assertEquals(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;),
	  	assertNotEquals : @xuanzi.h5.unittest.Asserts::assertNotEquals(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)
	  }
	 	$wnd.assertNull = @xuanzi.h5.unittest.Asserts::assertNull(Ljava/lang/String;Ljava/lang/Object;);
	  	$wnd.assertNotNull = @xuanzi.h5.unittest.Asserts::assertNotNull(Ljava/lang/String;Ljava/lang/Object;);
	  	$wnd.assertFalse = @xuanzi.h5.unittest.Asserts::assertFalse(Ljava/lang/String;Z);
	  	$wnd.assertTrue = @xuanzi.h5.unittest.Asserts::assertTrue(Ljava/lang/String;Z);
	  	$wnd.assertEquals = @xuanzi.h5.unittest.Asserts::assertEquals(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;);
	  	$wnd.assertNotEquals = @xuanzi.h5.unittest.Asserts::assertNotEquals(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;);
	}-*/;

}
